/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package COSC455Compiler;

/**
 *
 * @author Alexander
 */
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Stack;

public class MyCompiler {

    public static ArrayList<String> HTMLOutput = new ArrayList<String>();
    public static Stack<String> symbolStack = new Stack<String>();

    public static String token = "";
    public static String nextCharacter = ""; // Next Character
    public static int currentPosition = 0;
    public static String sourceLine = "";
    public static int currentTag = 0;
    public static String Filename = "";

    static MyLexicalAnalyzer Lexer;
    static MySyntaxAnalyzer Parser = new MySyntaxAnalyzer();
    static MySemanticAnalyzer Generator;

    static int count = 0;

    private static Scanner reader;
    
    public static void main(String[] args) {
        LexicalTokens.FillLexicalTokens();
        if (args.length != 1) {
            System.err.println("ERROR!");
            System.exit(0); // quit
        } else {
            Filename = args[0];
            int index = Filename.indexOf('.');
            
            // **Check extension
            if (Filename.substring(index).equalsIgnoreCase(File_Ext_Checker.Ext)) {
                try {
                    File file = new File(Filename);
                    reader = new Scanner(file);
                    while (reader.hasNext()) {
                        sourceLine = sourceLine + reader.nextLine() + " ";
                    }
                    sourceLine = sourceLine.replaceAll("\\r", "").replaceAll("\\n", "").replaceAll("\\t", "");

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            } else {
                System.err.println("ERROR! The file you have entered is not valid.");
                System.exit(0); // quit
            }
        }

        Lexer = new MyLexicalAnalyzer();
        Generator = new MySemanticAnalyzer();
        MyCompiler.Parser.lolcode();

        String output = "";

        // **Call semantic analyzer
        for (int i = 0; i < HTMLOutput.size(); i++) {
            output = output + HTMLOutput.get(i) + "";
        }
        System.out.print(output);

        try {
            File file = new File("output.html");
            String path = file.getAbsolutePath();
            BufferedWriter out = new BufferedWriter(new FileWriter(file));
            out.write(output);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // **Write output file
    }
}
